<template>
  <el-dialog :visible="showDialog" title="新增员工" @close="cancel">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="姓名" label-width="100px" prop="username">
        <el-input v-model="form.username" autocomplete="off" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item label="手机" label-width="100px" prop="mobile">
        <el-input v-model="form.mobile" autocomplete="off" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" label-width="100px" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="转正时间" label-width="100px" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" label-width="100px" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择活动区域">
          <el-option label="正式" :value="1">正式</el-option>
          <el-option label="非正式" :value="2">非正式</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" label-width="100px" prop="workNumber">
        <el-input v-model="form.workNumber" autocomplete="off" placeholder="1-20个数字" />
      </el-form-item>
      <el-form-item label="部门" label-width="100px" prop="departmentName">
        <el-input v-model="form.departmentName" autocomplete="off" placeholder="请选择" @focus="getDepartments" />
        <div class="bigTree">
          <el-tree
            v-if="showTree"
            :data="treeData"
            default-expand-all=""
            :props="{ label: 'name' }"
            class="tree"
            @node-click="selectNode"
          />
        </div>
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="center">
      <el-button type="primary" @click="addEmployee">提交</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
import { validMobile } from '@/utils/validate'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean
    }
  },
  data() {
    const validateMobile = (rule, value, callback) => {
      console.log(value)
      if (validMobile(value)) {
        callback()
      } else {
        callback(new Error('手机号码格式错误'))
      }
    }
    const checkTime = (rule, value, callback) => {
      if (!this.form.timeOfEntry || !this.form.correctionTime) {
        callback()
      } else {
        const timeOfEntryTamp = this.form.timeOfEntry.getTime()
        console.log(timeOfEntryTamp)
        const correctionTimeTamp = this.form.correctionTime.getTime()
        console.log(correctionTimeTamp)
        timeOfEntryTamp - correctionTimeTamp < 0 ? callback() : callback(new Error('转正时间不能在入职时间之前'))
      }
    }
    return {
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        correctionTime: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: ''
      },
      treeData: [],
      showTree: false,
      rules: {
        username: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '1-30个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        timeOfEntry: [ // 入职时间
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: checkTime, trigger: 'change' },
          { validator: checkTime, trigger: 'blur' }

        ],
        correctionTime: [ // 转正事件
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: checkTime, trigger: 'change' },
          { validator: checkTime, trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '1-20个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      this.treeData = transListToTreeData(depts, '')
      console.log(this.treeData)
      this.showTree = true
    },
    selectNode(node) {
      this.form.departmentName = node.name
      this.showTree = false
    },
    async addEmployee() {
      try {
        const isOk = await this.$refs.form.validate()
        if (isOk) {
          const res = addEmployee(this.form)
          console.log(res)
        }
        this.$emit('addEmployee')
        await this.$refs.form.resetFields()
      } catch (error) {
        console.log(error)
      }
    },
    cancel() {
      this.form = {
        username: '',
        mobile: '',
        timeOfEntry: '',
        correctionTime: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss">
    .tree{
        left: 0;
        height: 300px;
        width: 200px;
        overflow-y: scroll;
    }
    .bigTree{
        position: absolute;
        width: 180px;
        height: 300px;
        overflow: hidden;
        z-index: 999;
    }
</style>
